﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CommonCompareTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        CompareGroup group = new CompareGroup();
        ComparePair curretnPair;
        private void button_SetCompareList_Click(object sender, EventArgs e)
        {
            List<string> list = GetList();

            group.SetCompareItems(list);
            group.CompareEvent += new CompareHandle(group_CompareEvent);
            group.CompareCompleted += new CompareCompeltedHandle(group_CompareCompleted);
            group.StartCompare();
        }

        void group_CompareCompleted(CompareRank rank)
        {
            MessageBox.Show(rank.ToString());
            Clipboard.SetText(rank.ToString());
        }

        void group_CompareEvent(ComparePair pair, int index, int SumCount)
        {
            displaypair(pair);
            displayindex(index,  SumCount);
        }

        private void displayindex(int index, int SumCount)
        {
            label1.Text = index.ToString()+"/" + SumCount.ToString();
        }

        private void displaypair(ComparePair pair)
        {
            curretnPair = pair;
            button_ItemA.Text = pair.ItemA.Item;
            button_ItemB.Text = pair.ItemB.Item;
        }








        private List<string> GetList()
        {
            List<string> list = new List<string>();

            //foreach (string item in textBox1.Lines)
            //{
            //    list.Add(item);
            //}
            return textBox1.Lines.Where<string>(x => !string.IsNullOrEmpty(x)).ToList<string>();
            // return list;
        }

        private void button_Load_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = openFileDialog1.FileName;

                List<string> list = LoadTxtFileList(path);

                SetListToUI(list);
            }
        }

        private List<string> LoadTxtFileList(string path)
        {
            List<string> list = new List<string>();

            if (string.IsNullOrEmpty(path))
            {
                return list;
            }
            FileInfo myFile = new FileInfo(path);

            StreamReader sr = myFile.OpenText();

            while (!sr.EndOfStream)
            {
                list.Add(sr.ReadLine());
            }

            return list;


        }

        private void SetListToUI(List<string> list)
        {
            list.ForEach(x => textBox1.Text += x + "\r\n");
        }

        private void button_ItemA_Click(object sender, EventArgs e)
        {
            group.SetCompareResult(CompareResult.FirstOneIsBetter);
        }

        private void button_ItemB_Click(object sender, EventArgs e)
        {
            group.SetCompareResult(CompareResult.FirstOneIsBetter);
        }

        private void button_Equal_Click(object sender, EventArgs e)
        {
            group.SetCompareResult(CompareResult.Equal);
        }

        private void button_BothLike_Click(object sender, EventArgs e)
        {
            group.SetCompareResult(CompareResult.BothLike);
        }

        private void button_BothDisklike_Click(object sender, EventArgs e)
        {
            group.SetCompareResult(CompareResult.BothDisLike);
        }
    }
}
